<template>
  <div>
    <el-form
      ref="mewsForm"
      :inline="true"
      :model="mewsInfo"
      :class="required == '1' ? 'require' : 'notRequire'"
      class="demo-form-inline pageForm"
      label-position="right"
      label-width="120px"
      size="small"
      :rules="mewsRules"
    >
      <el-row type="flex" justify="end" class="button-save">
        <el-button type="primary" @click="save">保存</el-button>
      </el-row>
      <el-row class="mews">
        <el-form-item label="意识" prop="consciousnessNote">
          <el-select v-model="mewsInfo.consciousnessNote" clearable>
            <el-option label="清醒" value="0" />
            <el-option label="对语言有反应" value="1" />
            <el-option label="对刺痛有反应" value="2" />
            <el-option label="对任何刺激无反应" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="呼吸" class="sixty_percent" prop="rr">
          <el-input v-model="mewsInfo.rr">
            <template slot="append"> 次/分 </template>
          </el-input>
        </el-form-item>
        <el-form-item label="脉搏" class="sixty_percent" prop="pr">
          <el-input v-model="mewsInfo.pr">
            <template slot="append"> 次/分 </template>
          </el-input>
        </el-form-item>
        <el-form-item label="心率" class="sixty_percent" prop="hr">
          <el-input v-model="mewsInfo.hr">
            <template slot="append"> 次/分 </template>
          </el-input>
        </el-form-item>
        <el-form-item label="收缩压" class="sixty_percent" prop="sys">
          <el-input v-model="mewsInfo.sys">
            <template slot="append"> mmHg </template>
          </el-input>
        </el-form-item>
        <el-form-item label="舒张压" class="sixty_percent" prop="dia">
          <el-input v-model="mewsInfo.dia">
            <template slot="append"> mmHg </template>
          </el-input>
        </el-form-item>
        <el-form-item label="体温" class="sixty_percent" prop="t1">
          <el-input v-model="mewsInfo.t1">
            <template slot="append"> ℃ </template>
          </el-input>
        </el-form-item>
        <el-form-item label="spo2" class="sixty_percent" prop="spo2">
          <el-input v-model="mewsInfo.spo2">
            <template slot="append"> % </template>
          </el-input>
        </el-form-item>
        <el-form-item label="MEWS" style="width: 100%" prop="type">
          <el-input v-model="mewsInfo.type" style="width: 500px" readonly />
        </el-form-item>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  getClinicalSignDatas,
  saveClinicalSignDatas
} from '@/api/triage/triage-new'

export default {
  name: 'TriageMews',
  props: {
    id: {
      type: Object,
      default: () => {}
    },
    require: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      mewsInfo: {
        type: ''
      },
      required: '',
      MEWSScoreTable: {
        rr: [
          { greater: '', less: '<=8', score: '2' },
          { greater: '>=9', less: '<=14', score: '0' },
          { greater: '>=15', less: '<=20', score: '1' },
          { greater: '>=21', less: '<=29', score: '2' },
          { greater: '>=30', less: '', score: '3' }
        ],
        hr: [
          { greater: '', less: '<=40', score: '2' },
          { greater: '>=41', less: '<=50', score: '1' },
          { greater: '>=51', less: '<=100', score: '0' },
          { greater: '>=101', less: '<=110', score: '1' },
          { greater: '>=111', less: '<=129', score: '2' },
          { greater: '>=130', less: '', score: '3' }
        ],
        sys: [
          { greater: '', less: '<=70', score: '3' },
          { greater: '>=71', less: '<=80', score: '2' },
          { greater: '>=81', less: '<=100', score: '1' },
          { greater: '>=101', less: '<=199', score: '0' },
          { greater: '>=200', less: '', score: '2' }
        ],
        t1: [
          { greater: '', less: '<=35', score: '2' },
          { greater: '>=35.1', less: '<=36', score: '1' },
          { greater: '>=36.1', less: '<=38', score: '0' },
          { greater: '>=38.1', less: '<=38.5', score: '1' },
          { greater: '>=38.6', less: '', score: '2' }
        ]
      },
      mewsRules: {
        consciousnessNote: null,
        rr: null,
        pr: null,
        hr: null,
        sys: null,
        dia: null,
        t1: null,
        spo2: null,
        type: null
      }
    }
  },
  watch: {
    mewsInfo: {
      handler() {
        this.mewsInfo.type = this.getMewsScore().grade
      },
      deep: true
    }
  },
  created() {
    this.getClinicalSignDatas()
    this.setRequire()
  },
  methods: {
    getClinicalSignDatas() {
      getClinicalSignDatas({ dispatchid: this.id.dispatchId }).then((res) => {
        if (res.code == 0 && res.data) {
          this.mewsInfo = res.data
        }
      })
    },
    setRequire() {
      if (this.require.length > 0) {
        this.required = this.require[0].value
        if (this.required !== '2') {
          Object.keys(this.mewsRules).forEach((el) => {
            this.mewsRules[el] = [
              {
                required: true,
                message: ' ',
                trigger: 'blur'
              }
            ]
          })
        } else {
          Object.keys(this.mewsRules).forEach((el) => {
            this.mewsRules[el] = null
          })
        }
      }
    },
    getMewsScore() {
      let flag = false
      let arr = []
      let grade = ''
      Object.keys(this.MEWSScoreTable).forEach((el) => {
        if (this.mewsInfo[el]) {
          flag = true
          for (let i = 0; i < this.MEWSScoreTable[el].length; i++) {
            let greater = this.calculator(
              this.mewsInfo[el] + this.MEWSScoreTable[el][i].greater
            )

            let less = this.calculator(
              this.mewsInfo[el] + this.MEWSScoreTable[el][i].less
            )

            if (greater && less) {
              if (Number(this.MEWSScoreTable[el][i].score) > 2) {
                grade = '呼叫医生'
              }
              arr.push(this.MEWSScoreTable[el][i].score)
            }
          }
        }
      })
      if (this.mewsInfo.consciousnessNote) {
        if (parseInt(this.mewsInfo.consciousnessNote) > 2) {
          grade = '呼叫医生'
        }
        flag = true

        arr.push(this.mewsInfo.consciousnessNote)
      }
      if (flag) {
        let sum = this.calculator(arr.join('+'))
        if (sum >= 4) {
          grade = '呼叫医生'
        }
        return { score: sum, grade: grade }
      } else {
        return ''
      }
    },
    calculator(str) {
      return new Function('return (' + str + ')')()
    },
    save() {
      this.$nextTick(() => {
        this.$refs.mewsForm.validate((valid) => {
          if (!valid && this.required == '1') {
            this.$message.error('请通过验证')
          } else {
            saveClinicalSignDatas({ ...this.mewsInfo, ...this.id }).then(
              (res) => {
                if (res.code == 0) {
                  this.$message.success('保存成功')
                  this.getClinicalSignDatas()
                } else {
                  this.$message.error('保存失败')
                }
              }
            )
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.mews {
  .el-form-item--small.el-form-item {
    margin-bottom: 22px;
  }
  .el-form-item {
    width: 400px;
    .el-select {
      width: 255px;
    }
    .el-input {
      width: 255px;
    }
  }
}
.button-save {
  padding-right: 40px;
  margin-bottom: 20px;
}
</style>

<style lang="scss">
.notRequire {
  .el-form-item.is-error .el-input__inner {
    border-color: #dcdfe6;
  }
}
</style>